package com.qfmy.webapp.service.impl.novel;

import com.qfmy.common.exception.BaseException;
import com.qfmy.common.login.LoginUser;
import com.qfmy.common.login.LoginUserHolder;
import com.qfmy.common.result.ResultCodeEnum;
import com.qfmy.model.entity.novel.CommentReplies;
import com.qfmy.model.vo.novel.CommentRepliesVo;
import com.qfmy.webapp.mapper.novel.CommentRepliesMapper;
import com.qfmy.webapp.mapper.novel.NovelCommentMapper;
import com.qfmy.webapp.service.agent.ChatService;
import com.qfmy.webapp.service.novel.CommentRepliesService;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 清风明月
 * @Package com.qfmy.web.app.service.impl.novel
 * @date 2025/11/13
 * @description 评论回复表
 */
@Slf4j
@Service
public class CommentRepliesServiceImpl implements CommentRepliesService {

    /**
     * 注入mapper
     */
    @Autowired
    private CommentRepliesMapper commentRepliesMapper;

    /**
     * 注入评论mapper
     */
    @Autowired
    private NovelCommentMapper novelCommentMapper;

    /**
     * 注入chatService
     */
    @Autowired
    private ChatService chatService;

    /**
     * 添加评论回复
     * @param commentReplies
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCommentReplies(@NotNull CommentReplies commentReplies) {
        //校验回复内容是否包含敏感信息
        chatService.checkByAi(commentReplies.getContent());
        //添加评论回复
        commentRepliesMapper.addCommentReplies(commentReplies);
        //更新评论回复数
        Integer count = novelCommentMapper.updateReplyCount(commentReplies.getParentCommentId());
        //判断评论回复数是否为0
        if(count == 0)
        {
            log.error("评论回复数更新失败,父评论可能已经被删除了");
            throw new BaseException(ResultCodeEnum.COMMENT_NOT_EXIST);
        }
    }

    /**
     * 根据评论ID获取评论回复
     * @return
     */
    @Override
    public List<CommentRepliesVo> getCommentRepliesByCommentId(Integer commentId) {
        //获取当前用户id
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        //判断当前用户是否登录
        if(loginUser == null)
        {
            throw new BaseException(ResultCodeEnum.TOKEN_INVALID);
        }
        //获取用户id
        Long userId = loginUser.getUserId();
        //根据评论ID获取评论回复
        List<CommentReplies> commentReplies = commentRepliesMapper.getCommentRepliesByCommentId(commentId,userId);
        //进行对象的转换
        return convert(commentReplies);
    }

    /**
     * 删除评论回复
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCommentReplies(Integer id) {
        //删除评论回复
        commentRepliesMapper.deleteCommentReplies(id);
        //更新评论回复数
        novelCommentMapper.updateReplyCountRec(id);
    }

    /**
     * 判断当前用户是否已经回复过
     * @param commentId
     * @return
     */
    @Override
    public Boolean isCommentReplies(Integer commentId) {
        //获取当前用户id
        Long userId = getCurrentUser();
        //判断当前用户是否已经回复过
        Integer count = commentRepliesMapper.selectCommentById(commentId,userId);
        //返回
        return count == 1;
    }

    /**
     * 获取当前用户
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private Long getCurrentUser() {
        //判断当前用户是否登录
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        if (loginUser == null) {
            throw new BaseException(ResultCodeEnum.TOKEN_INVALID);
        }
        //获取用户id
        return loginUser.getUserId();
    }

    /**
     * 对象转换
     * @param commentReplies
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private List<CommentRepliesVo> convert(List<CommentReplies> commentReplies) {
        //判断集合是否为空
        if (commentReplies != null && !commentReplies.isEmpty()) {
            return commentReplies.stream().map(commentReply -> {
                //创建对象
                CommentRepliesVo commentRepliesVo = new CommentRepliesVo();
                //进行对象赋值
                BeanUtils.copyProperties(commentReply, commentRepliesVo);
                return commentRepliesVo;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}
